Skip to content

Write ignored lines to cache meta#20747

Merged
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:fix-warm-import-ignores
Feb 6, 2026
Merged

Write ignored lines to cache meta#20747
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:fix-warm-import-ignores

Conversation

@ilevkivskyi
Copy link
Member

This fixes a bug discovered while working on #20742, after all it turns out this is not related to #20105 (which is caused by a bug when using --no-namespace-packages with namespace packages).

This is a bit unfortunate, since cache metas are already quite bulky, but I don't see another way, since we emit import errors during graph loading, before the caller/importer is either parsed or deserialized. So we don't know if there are any type ignores in the caller.

Note only the first test I added was broken, the second is just a mirror image of the first test I add just in case.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Ping on this one.

@sterliakov
Copy link
Collaborator

Did you try to look at the physical cache size before&after? Is there any significant impact? (mypy would be a bad benchmark as there are almost no ignores here, but some medium repo from primer should do)

Would it make sense to restrict this cache write to imports - not error codes, but comments attached to import statements - to reduce the cache impact?

@ilevkivskyi
Copy link
Member Author

It is like extra ~10 bytes per type ignore. Typical cache meta size is like 1kB, so this becomes visible if you have more than a dozen ignores per file.

Would it make sense to restrict this cache write to imports

Hm, actually if it is easy to do, I will try to filter (using dep_lines).

write_int_bare(data, len(self.ignored_lines))
for line, codes in self.ignored_lines.items():
write_int(data, line)
write_str_list(data, codes)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this becomes a bottleneck, we could use an integer id to represent some of most common error codes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we could potentially filter common error codes that we know won't be generated for import related errors, but this is a bit dangerous, as we'd need to ensure these ignored lines are used for cases where they work. We could rename the variable though to reduce confusion.

In any case, maye this won't be an issue -- we should probably only optimized if this appears to have some impact in real-world projects.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will probably just do some easy optimization(s) for now (and will probably rename it to something like import_ignore_lines).

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi merged commit 296e609 into python:master Feb 6, 2026
23 checks passed
@ilevkivskyi ilevkivskyi deleted the fix-warm-import-ignores branch February 6, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants